import moment from "moment";

export function fixedZero(val) {
  return val * 1 < 10 ? `0${val}` : val;
}

export function getTimeDistance(type) {
  const now = new Date();
  const oneDay = 1000 * 60 * 60 * 24;

  if (type === "today") {
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  }

  if (type === "week") {
    let day = now.getDay();
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);

    if (day === 0) {
      day = 6;
    } else {
      day -= 1;
    }

    const beginTime = now.getTime() - day * oneDay;

    return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  }

  if (type === "month") {
    const year = now.getFullYear();
    const month = now.getMonth();
    const nextDate = moment(now).add(1, "months");
    const nextYear = nextDate.year();
    const nextMonth = nextDate.month();

    return [
      moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
      moment(
        moment(
          `${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`
        ).valueOf() - 1000
      )
    ];
  }

  if (type === "year") {
    const year = now.getFullYear();

    return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  }
}

export function getPlainNode(nodeList, parentPath = "") {
  const arr = [];
  nodeList.forEach(node => {
    const item = node;
    item.path = `${parentPath}/${item.path || ""}`.replace(/\/+/g, "/");
    item.exact = true;
    if (item.children && !item.component) {
      arr.push(...getPlainNode(item.children, item.path));
    } else {
      if (item.children && item.component) {
        item.exact = false;
      }
      arr.push(item);
    }
  });
  return arr;
}

export function digitUppercase(n) {
  const fraction = ["角", "分"];
  const digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  const unit = [["元", "万", "亿"], ["", "拾", "佰", "仟"]];
  let num = Math.abs(n);
  let s = "";
  fraction.forEach((item, index) => {
    s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(
      /零./,
      ""
    );
  });
  s = s || "整";
  num = Math.floor(num);
  for (let i = 0; i < unit[0].length && num > 0; i += 1) {
    let p = "";
    for (let j = 0; j < unit[1].length && num > 0; j += 1) {
      p = digit[num % 10] + unit[1][j] + p;
      num = Math.floor(num / 10);
    }
    s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
  }

  return s
    .replace(/(零.)*零元/, "元")
    .replace(/(零.)+/g, "零")
    .replace(/^整$/, "零元整");
}

//查询参数
export function gup(name, url) {
  if (!url) url = location.href;
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  const regexS = "[\\?&]" + name + "=([^&#]*)";
  const regex = new RegExp(regexS);
  const results = regex.exec(url);
  return results == null ? null : results[1];
}

/**
 * 计算日期年月日
 * @date RangePicker值
 */
export function getDateInterval(date) {
  const startminTime = (new Date(date[0]['_d'])).getTime();
  const endminTime = (new Date(date[1]['_d'])).getTime();

  const currentStartYear = moment(startminTime).format('YYYY');
  const currentEndYear = moment(endminTime).format('YYYY');
  const currentStartMonth = moment(startminTime).format('MM');
  const currentEndMonth = moment(endminTime).format('MM');
  const currentStartDay = moment(startminTime).format('DD');
  const currentEndDay = moment(endminTime).format('DD');

  let year;
  let month;
  let day;
  if (currentEndYear >= currentStartYear) {
    if (currentEndMonth >= currentStartMonth) {
      year = currentEndYear - currentStartYear;
      if (currentEndDay >= currentStartDay) {
        month = currentEndMonth - currentStartMonth;
        day = currentEndDay - currentStartDay;
      } else {
        month = currentEndMonth - currentStartMonth - 1;
        day = currentEndDay - currentStartDay + 30;
      }
    } else {
      year = currentEndYear - currentStartYear - 1;
      if (currentEndDay >= currentStartDay) {
        month = currentEndMonth - currentStartMonth + 12;
        day = currentEndDay - currentStartDay;
      } else {
        month = currentEndMonth - currentStartMonth + 12 - 1;
        day = currentEndDay - currentStartDay + 30;
      }
    }
  }
  console.log("年", year);
  console.log("月", month);
  console.log("日", day);
  console.log("nianyueri", year + '年' + month + '月' + day + '天');
  let timeLimit;
  if (year === 0) {
    if (month === 0) {
      timeLimit = day + '天';
      if (day === 0) {
        timeLimit = '';
      }
    } else {
      timeLimit = month + '月' + day + '天';
    }
  } else {
    timeLimit = year + '年' + month + '月' + day + '天';
  }
  console.log("jutishijian timeLimit", timeLimit);
  return timeLimit;
}